home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_222 / plplot / src / source.zoo / plbox.c < prev    next >
C/C++ Source or Header  |  1989-05-15  |  12KB  |  430 lines

  1. /* This draws a box around the current viewport. XOPT and YOPT are */
  2. /* character strings which define the box as follows: */
  3.  
  4. /* A: Draw axis (X is horizontal line Y=0, Y is vertical line X=0) */
  5. /* B: Draw bottom (X) or left (Y) edge of frame */
  6. /* C: Draw top (X) or right (Y) edge of frame */
  7. /* G: Draws a grid at the major tick interval */
  8. /* I: Inverts tick marks */
  9. /* L: Logarithmic axes, major ticks at decades, minor ticks at units */
  10. /* N: Write numeric label at conventional location */
  11. /* M: Write numeric label at unconventional location */
  12. /* T: Draw major tick marks */
  13. /* S: Draw minor tick marks */
  14. /* V: (for Y only) Label vertically */
  15.  
  16. /* xtick, ytick are the major tick intervals required, zero for  */
  17. /*              automatic selection */
  18. /* nxsub, nysub are the number of subtick intervals in a major tick */
  19. /*              interval */
  20.  
  21. #include "plplot.h"
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <string.h>
  25.  
  26. #define betw(c,a,b) ((a<=c && c<=b) || (b<=c && c<=a))
  27.  
  28. static float xlog[8] = {0.301030, 0.477121, 0.602060, 0.698970,
  29.                         0.778151, 0.845098, 0.903090, 0.954243};
  30.  
  31. void plbox(xopt,xtick,nxsub,yopt,ytick,nysub)
  32. char xopt[],yopt[];
  33. float xtick, ytick;
  34. int nxsub, nysub;
  35. {
  36.       char string[40];
  37.       char strtmp[4];
  38.       int lax,lbx,lcx,lgx,lix,llx,lmx,lnx,lsx,ltx;
  39.       int lay,lby,lcy,lgy,liy,lly,lmy,lny,lsy,lty,lvy;
  40.       int xmajor, xminor, ymajor, yminor, xmode, xprec;
  41.       int ymode, yprec;
  42.       int i, i1x, i2x, i3x, i4x, i1y, i2y, i3y, i4y, it0;
  43.       int nxsub1, nysub1;
  44.       int lxmin, lxmax, lymin, lymax;
  45.       int pxmin, pxmax, pymin, pymax;
  46.       int vppxmi, vppxma, vppymi, vppyma;
  47.       int level;
  48.       float xpmm, ypmm, defmaj, defmin, htmaj, htmin;
  49.       float xtick1, ytick1, vpwxmi, vpwxma, vpwymi, vpwyma;
  50.       float pos, tn, tp, temp;
  51.  
  52.       glev(&level);
  53.       if (level<3) fatal("Please set up window before calling PLBOX.");
  54.  
  55. /* Open  the clip limits to the subpage limits */
  56.  
  57.       gclp(&lxmin,&lxmax,&lymin,&lymax);
  58.       gphy(&pxmin,&pxmax,&pymin,&pymax);
  59.       sclp(pxmin,pxmax,pymin,pymax);
  60.  
  61.       gvpp(&vppxmi,&vppxma,&vppymi,&vppyma);
  62.  
  63. /* Tick and subtick sizes in device coords */
  64.  
  65.       gpixmm(&xpmm,&ypmm);
  66.       gmaj(&defmaj,&htmaj);
  67.       gmin(&defmin,&htmin);
  68.       
  69.       xmajor=max(round(htmaj*ypmm),1);
  70.       ymajor=max(round(htmaj*xpmm),1);
  71.       xminor=max(round(htmin*ypmm),1);
  72.       yminor=max(round(htmin*xpmm),1);
  73.  
  74.       xtick1=xtick;
  75.       nxsub1=nxsub;
  76.       ytick1=ytick;
  77.       nysub1=nysub;
  78.  
  79.       lax=strpos(xopt,'A')!=-1 || strpos(xopt,'a')!=-1;
  80.       lbx=strpos(xopt,'B')!=-1 || strpos(xopt,'b')!=-1;
  81.       lcx=strpos(xopt,'C')!=-1 || strpos(xopt,'c')!=-1;
  82.       lgx=strpos(xopt,'G')!=-1 || strpos(xopt,'g')!=-1;
  83.       lix=strpos(xopt,'I')!=-1 || strpos(xopt,'i')!=-1;
  84.       llx=strpos(xopt,'L')!=-1 || strpos(xopt,'l')!=-1;
  85.       lmx=strpos(xopt,'M')!=-1 || strpos(xopt,'m')!=-1;
  86.       lnx=strpos(xopt,'N')!=-1 || strpos(xopt,'n')!=-1;
  87.       lsx=strpos(xopt,'S')!=-1 || strpos(xopt,'s')!=-1;
  88.       ltx=strpos(xopt,'T')!=-1 || strpos(xopt,'t')!=-1;
  89.  
  90.       lay=strpos(yopt,'A')!=-1 || strpos(yopt,'a')!=-1;
  91.       lby=strpos(yopt,'B')!=-1 || strpos(yopt,'b')!=-1;
  92.       lcy=strpos(yopt,'C')!=-1 || strpos(yopt,'c')!=-1;
  93.       lgy=strpos(yopt,'G')!=-1 || strpos(yopt,'g')!=-1;
  94.       liy=strpos(yopt,'I')!=-1 || strpos(yopt,'i')!=-1;
  95.       lly=strpos(yopt,'L')!=-1 || strpos(yopt,'l')!=-1;
  96.       lmy=strpos(yopt,'M')!=-1 || strpos(yopt,'m')!=-1;
  97.       lny=strpos(yopt,'N')!=-1 || strpos(yopt,'n')!=-1;
  98.       lsy=strpos(yopt,'S')!=-1 || strpos(yopt,'s')!=-1;
  99.       lty=strpos(yopt,'T')!=-1 || strpos(yopt,'t')!=-1;
  100.       lvy=strpos(yopt,'V')!=-1 || strpos(yopt,'v')!=-1;
  101.  
  102.       gvpw(&vpwxmi,&vpwxma,&vpwymi,&vpwyma);
  103.       lax=lax && (vpwymi*vpwyma<0.0) && !llx;
  104.       lay=lay && (vpwxmi*vpwxma<0.0) && !lly;
  105.       if (llx) xtick1=1.0;
  106.       if (lly) ytick1=1.0;
  107.       if (ltx || lgx) 
  108.           pldtik(vpwxmi,vpwxma,&xtick1,&nxsub1,&xmode,&xprec);
  109.       if (lty || lgy) 
  110.           pldtik(vpwymi,vpwyma,&ytick1,&nysub1,&ymode,&yprec);
  111.  
  112. /* Set up tick variables */
  113.  
  114.       if (lix) {
  115.         i1x=xminor;
  116.         i2x=0;
  117.         i3x=xmajor;
  118.         i4x=0;
  119.       }
  120.       else {
  121.         i1x=0;
  122.         i2x=xminor;
  123.         i3x=0;
  124.         i4x=xmajor;
  125.       }
  126.  
  127.       if (liy) {
  128.         i1y=yminor;
  129.         i2y=0;
  130.         i3y=ymajor;
  131.         i4y=0;
  132.       }
  133.       else {
  134.         i1y=0;
  135.         i2y=yminor;
  136.         i3y=0;
  137.         i4y=ymajor;
  138.       }
  139.  
  140. /* Draw the bottom edge of the box */
  141.  
  142.       if (lbx) {
  143.         movphy(vppxmi,vppymi);
  144.         if (ltx) {
  145.           tp=xtick1*floor(vpwxmi/xtick1);
  146. bedge:
  147.           tn=tp+xtick1;
  148.           if (lsx) {
  149.             if (llx) {
  150.               for(i=0; i<=7;i++){
  151.                 temp=tp+xlog[i];
  152.                 if (betw(temp,vpwxmi,vpwxma))
  153.                      plxtik(wcpcx(temp),vppymi,i1x,i2x);
  154.               }
  155.             }
  156.             else {
  157.               for ( i=1;i<=nxsub1-1;i++) {
  158.                 temp=tp+i*(tn-tp)/nxsub1;
  159.                 if (betw(temp,vpwxmi,vpwxma))
  160.                      plxtik(wcpcx(temp),vppymi,i1x,i2x);
  161.               }
  162.             }
  163.           }
  164.           temp=tn;
  165.           if (betw(temp,vpwxmi,vpwxma)) {
  166.             plxtik(wcpcx(temp),vppymi,i3x,i4x);
  167.             tp=tn;
  168.             goto bedge;
  169.           }
  170.         }
  171.         draphy(vppxma,vppymi);
  172.       }
  173.                                            
  174. /* Draw right-hand edge of box */
  175.  
  176.       if (lcy) {
  177.         movphy(vppxma,vppymi);
  178.         if (lty) {
  179.           tp=ytick1*floor(vpwymi/ytick1);
  180. redge:
  181.           tn=tp+ytick1;
  182.           if (lsy) {
  183.             if (lly) {
  184.               for(i=0;i<=7;i++) {
  185.                 temp=tp+xlog[i];
  186.                 if (betw(temp,vpwymi,vpwyma))
  187.                      plytik(vppxma,wcpcy(temp),i2y,i1y);
  188.               }
  189.             }
  190.             else {
  191.               for(i=1;i<=nysub1-1;i++) {
  192.                 temp=tp+i*(tn-tp)/nysub1;
  193.                 if (betw(temp,vpwymi,vpwyma))
  194.                      plytik(vppxma,wcpcy(temp),i2y,i1y);
  195.               }
  196.             }
  197.           }
  198.           temp=tn;
  199.           if (betw(temp,vpwymi,vpwyma)) {
  200.             plytik(vppxma,wcpcy(temp),i4y,i3y);
  201.             tp=tn;
  202.             goto redge;
  203.           }
  204.         }
  205.         draphy(vppxma,vppyma);
  206.       }
  207.  
  208. /* Draw the top edge of the box */
  209.  
  210.       if (lcx) {
  211.         movphy(vppxma,vppyma);
  212.         if (ltx) {
  213.           tp=xtick1*(floor(vpwxma/xtick1)+1);
  214. tedge:
  215.           tn=tp-xtick1;
  216.           if (lsx) {
  217.             if (llx) {
  218.               for(i=7;i>=0;i--) {
  219.                 temp=tn+xlog[i];
  220.                 if (betw(temp,vpwxmi,vpwxma))
  221.                      plxtik(wcpcx(temp),vppyma,i2x,i1x);
  222.               }
  223.             }
  224.             else {
  225.               for(i=nxsub1-1;i>=1;i--) {
  226.                 temp=tn+i*(tp-tn)/nxsub1;
  227.                 if (betw(temp,vpwxmi,vpwxma))
  228.                      plxtik(wcpcx(temp),vppyma,i2x,i1x);
  229.               }
  230.             }
  231.           }
  232.           temp=tn;
  233.           if (betw(temp,vpwxmi,vpwxma)) {
  234.             plxtik(wcpcx(temp),vppyma,i4x,i3x);
  235.             tp=tn;
  236.             goto tedge;
  237.           }
  238.         }
  239.         draphy(vppxmi,vppyma);
  240.       }
  241.  
  242. /* Draw left-hand edge of box */
  243.  
  244.       if (lby) {
  245.         movphy(vppxmi,vppyma);
  246.         if (lty) {
  247.           tp=ytick1*(floor(vpwyma/ytick1)+1);
  248. ledge:
  249.           tn=tp-ytick1;
  250.           if (lsy) {
  251.             if (lly) {
  252.               for(i=7;i>=0;i--) {
  253.                 temp=tn+xlog[i];
  254.                 if (betw(temp,vpwymi,vpwyma))
  255.                      plytik(vppxmi,wcpcy(temp),i1y,i2y);
  256.               }
  257.             }
  258.             else {
  259.               for(i=nysub1-1;i>=1;i--) {
  260.                 temp=tn+i*(tp-tn)/nysub1;
  261.                 if (betw(temp,vpwymi,vpwyma))
  262.                      plytik(vppxmi,wcpcy(temp),i1y,i2y);
  263.               }
  264.             }
  265.           }
  266.           temp=tn;
  267.           if (betw(temp,vpwymi,vpwyma)) {
  268.             plytik(vppxmi,wcpcy(temp),i3y,i4y);
  269.             tp=tn;
  270.             goto ledge;
  271.           }
  272.         }
  273.         draphy(vppxmi,vppymi);
  274.       }
  275.  
  276. /* Draw the horizontal axis */
  277.  
  278.       if (lax) {
  279.         it0=wcpcy(0.0);
  280.         movphy(vppxmi,it0);
  281.         if (ltx) {
  282.           tp=xtick1*floor(vpwxmi/xtick1);
  283. haxis:
  284.           tn=tp+xtick1;
  285.           if (lsx) {
  286.             if (llx) {
  287.               for(i=0;i<=7;i++) {
  288.                 temp=tp+xlog[i];
  289.                 if (betw(temp,vpwxmi,vpwxma))
  290.                      plxtik(wcpcx(temp),it0,xminor,xminor);
  291.               }
  292.             }
  293.             else {
  294.               for(i=1;i<=nxsub1-1;i++) {
  295.                 temp=tp+i*(tn-tp)/nxsub1;
  296.                 if (betw(temp,vpwxmi,vpwxma))
  297.                      plxtik(wcpcx(temp),it0,xminor,xminor);
  298.               }
  299.             }
  300.           }
  301.           temp=tn;
  302.           if (betw(temp,vpwxmi,vpwxma)) {
  303.             plxtik(wcpcx(temp),it0,xmajor,xmajor);
  304.             tp=tn;
  305.             goto haxis;
  306.           }
  307.         }
  308.         draphy(vppxma,it0);
  309.       }
  310.  
  311. /* Draw the vertical axis */
  312.  
  313.       if (lay) {
  314.         it0=wcpcx(0.0);
  315.         movphy(it0,vppymi);
  316.         if (lty) {
  317.           tp=ytick1*floor(vpwymi/ytick1);
  318. vaxis:
  319.           tn=tp+ytick1;
  320.           if (lsy) {
  321.             if (lly) {
  322.               for(i=0;i<=7;i++) {
  323.                 temp=tp+xlog[i];
  324.                 if (betw(temp,vpwymi,vpwyma))
  325.                    plytik(it0,wcpcy(temp),yminor,yminor);
  326.               }
  327.             }
  328.             else {
  329.               for(i=1;i<=nysub1-1;i++) {
  330.                 temp=tp+i*(tn-tp)/nysub1;
  331.                 if (betw(temp,vpwymi,vpwyma))
  332.                      plytik(it0,wcpcy(temp),yminor,yminor);
  333.               }
  334.             }
  335.           }
  336.           temp=tn;
  337.           if (betw(temp,vpwymi,vpwyma)) {
  338.             plytik(it0,wcpcy(temp),ymajor,ymajor);
  339.             tp=tn;
  340.             goto vaxis;
  341.           }
  342.         }
  343.         draphy(it0,vppyma);
  344.       }
  345.  
  346. /* Draw grid in x direction */
  347.  
  348.       if (lgx) {
  349.         tp=xtick1*floor(vpwxmi/xtick1);
  350. xgrid:
  351.           tn=tp+xtick1;
  352.           if (betw(tn,vpwxmi,vpwxma)) {
  353.             pljoin(tn,vpwymi,tn,vpwyma);
  354.             tp=tn;
  355.             goto xgrid;
  356.           }
  357.       }
  358.  
  359. /* Draw grid in y direction */
  360.  
  361.       if (lgy) {
  362.         tp=ytick1*floor(vpwymi/ytick1);
  363. ygrid:
  364.           tn=tp+ytick1;
  365.           if (betw(tn,vpwymi,vpwyma)) {
  366.             pljoin(vpwxmi,tn,vpwxma,tn);
  367.             tp=tn;
  368.             goto ygrid;
  369.           }
  370.       }
  371.  
  372. /* Write horizontal label(s) */
  373.  
  374.       if ((lmx || lnx) && ltx) {
  375.         tp=xtick1*floor(vpwxmi/xtick1);
  376. hlabel:
  377.         tn=tp+xtick1;
  378.         if (betw(tn,vpwxmi,vpwxma)) {
  379.           if (!llx)
  380.             plform(tn,xmode,xprec,string);
  381.           else {
  382.             sprintf(strtmp,"%-d",round(tn));
  383.             strcpy(string,"10\\u");
  384.             strcat(string,strtmp);
  385.           }
  386.           pos=(tn-vpwxmi)/(vpwxma-vpwxmi);
  387.           if (lnx) plmtex("b",1.5,pos,0.5,string);
  388.           if (lmx) plmtex("t",1.5,pos,0.5,string);
  389.           tp=tn;
  390.           goto hlabel;
  391.         }
  392.       }
  393.  
  394. /* Write vertical label(s) */
  395.  
  396.       if ((lmy || lny) && lty) {
  397.         tp=ytick1*floor(vpwymi/ytick1);
  398. vlabel:
  399.         tn=tp+ytick1             ;
  400.         if (betw(tn,vpwymi,vpwyma)) {
  401.           if (!lly) 
  402.             plform(tn,ymode,yprec,string);
  403.           else {
  404.             sprintf(strtmp,"%-d",round(tn));
  405.             strcpy(string,"10\\u");
  406.             strcat(string,strtmp);
  407.           }
  408.           pos=(tn-vpwymi)/(vpwyma-vpwymi);
  409.           if (lny) {
  410.             if (lvy)
  411.               plmtex("lv",0.5,pos,1.0,string);
  412.             else
  413.               plmtex("l",1.5,pos,0.5,string);
  414.           }
  415.           if (lmy) {
  416.             if (lvy) 
  417.               plmtex("rv",0.5,pos,0.0,string);
  418.             else
  419.               plmtex("r",1.5,pos,0.5,string);
  420.           }
  421.           tp=tn;
  422.           goto vlabel;
  423.         }
  424.       }
  425.  
  426. /* Restore the clip limits to viewport edge */
  427.  
  428.       sclp(lxmin,lxmax,lymin,lymax);
  429. }
  430.